package com.example.buttomview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import kotlinx.android.synthetic.main.account_layout.*
import kotlinx.android.synthetic.main.contact_icon_layout.*
import kotlinx.android.synthetic.main.explore_icon_layout.*
import kotlinx.android.synthetic.main.message_icon_layout.*

class MainActivity : AppCompatActivity() {
    private lateinit var navController: NavController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //映射
        val destinatioanMap = mapOf(
            R.id.messageFragment to messageMotionLayout,
            R.id.contactFragment to contactMotionLayout,
            R.id.exploreFragment to exploreMotionLayout,
            R.id.accountFragment to accountMotionLayout
        )

        navController = findNavController(R.id.fragment)

        //解决AppBarConfiguration爆红，在依赖中添加
        // kotlinOptions{
        //        jvmTarget = 1.8
        //    }
        //设置标题栏内容和页面标题内容同步
        setupActionBarWithNavController(
            navController,
            AppBarConfiguration(
//                setOf(
//                    R.id.messageFragment,
//                    R.id.contactFragment,
//                    R.id.exploreFragment,
//                    R.id.accountFragment
//                )
            destinatioanMap.keys
            )
        )
        //设置message动画布局的点击监听,导航都message页面,实现页面切换
        //但不能显示图标切换的动画

        destinatioanMap.forEach{map->
            map.value.setOnClickListener { navController.navigate(map.key) }
        }
//        messageMotionLayout.setOnClickListener { navController.navigate(R.id.messageFragment) }
//
//        contactMotionLayout.setOnClickListener { navController.navigate(R.id.contactFragment) }
//
//        exploreMotionLayout.setOnClickListener { navController.navigate(R.id.exploreFragment) }
//        accountMotionLayout.setOnClickListener { navController.navigate(R.id.accountFragment) }

        //以下代码实现点击图标实现motion中的动画
        //给导航的目的地设置监听
        navController.addOnDestinationChangedListener { controller, destination, arguments ->
            //返回栈清空（页面点击一次返回键就退出）
            controller.popBackStack()
            //如果动画有延迟或异常，可以将0设置很小的值，如0.01
            //初始状态
            destinatioanMap.values.forEach { it.progress = 0f }
//            messageMotionLayout.progress = 0f
//            contactMotionLayout.progress = 0f
//            exploreMotionLayout.progress = 0f
//            accountMotionLayout.progress = 0f

           destinatioanMap[destination.id]?.transitionToEnd()
//            when (destination.id) {
//                R.id.messageFragment -> messageMotionLayout.transitionToEnd()
//                R.id.contactFragment -> contactMotionLayout.transitionToEnd()
//                R.id.exploreFragment -> exploreMotionLayout.transitionToEnd()
//                else -> accountMotionLayout.transitionToEnd()
//            }
        }
    }
}